package com.zb.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author WEN
 * @Date 2023/7/7 0007
 **/
@Data
@ApiModel(value = "返回结果封装")
public class Result {
    @ApiModelProperty(value = "状态码")
    private Long code;
    @ApiModelProperty(value = "是否成功：true-成功，false-失败")
    private boolean success;
    @ApiModelProperty(value = "返回消息")
    private String message;
    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data = new HashMap<>();

    public Result data(Map<String, Object> data) {
        this.data = data;
        return this;
    }

    public Result data(String k, Object v) {
        this.data.put(k, v);
        return this;
    }

    private Result(Long code, boolean success, String message){
        this.code = code;
        this.success = success;
        this.message = message;
    }

    public static Result success() {
        return new Result(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getSuccess(), ResultCode.SUCCESS.getMessage());
    }

    public static Result success(String message) {
        return new Result(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getSuccess(), StringUtils.isNotBlank(message)? message : ResultCode.SUCCESS.getMessage());
    }

    public static Result error(Long code, String message) {
        return new Result(code, false, message);
    }



}
